package com.uzai.console.service.tb.tborder.impl;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.mapper.*;
import com.uzai.console.service.tb.tborder.TbOrderApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营商基本配置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class TbOrderApiServiceImpl implements TbOrderApiService {

    Logger logger = LoggerFactory.getLogger(TbOrderApiServiceImpl.class);

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 通过商品id查询店铺信息
     * @author jitwxs
     * @date 2021/10/25 11:40
     * @param itemId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> findStore(String itemId){
        try {
            Map<String,Object> result = new HashMap<>();
            String appkey = uzaiConsoleSysConfig.getTbAppkey();
            String appsecret = uzaiConsoleSysConfig.getTbAppsecret();
            TaobaoClient client = new DefaultTaobaoClient(uzaiConsoleSysConfig.getTbApiUrl(), appkey, appsecret);
            TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
            req.setNumIids(itemId);
            TbkItemInfoGetResponse rsp = client.execute(req);
            if (rsp!=null){
                List<TbkItemInfoGetResponse.NTbkItem> items = rsp.getResults();
                if (items!=null){
                    TbkItemInfoGetResponse.NTbkItem item = items.get(0);
                    if (item!=null){
                        Long sellerId = item.getSellerId();
                        String nick = item.getNick();
                        result.put("storeId",sellerId);
                        result.put("storeName",nick);
                    }
                }
            }
            return result;
        } catch (ApiException e) {
            logger.error("get store err:{}",e);
        }
        return null;
    }




}
